<!--
 * @Author: wangming
 * @Date: 2020-09-07 16:57:20
 * @LastEditors: wangming
 * @LastEditTime: 2022-06-01 09:49:50
 * @Description: 应用端form组件内部唯一入口 负责控制显示隐藏,确认依赖的表单已经加载完成后才加载自己
-->
<template>
  <component
    :config="config"
    :form="form"
    :descForm="descForm"
    :is="config.__config.type === 'rowLayout' ? 'rowLayout' : 'ruleItem'"
    :seniorShow="seniorShow"
  ></component>
</template>

<script>
import storeform from "@/store";
const ruleItem = () => import("./formItem/ruleItem.vue");
const rowLayout = () => import("./__layout/index.vue");
import RunFormat from "@/utils/runFormatMap";
import { debounce } from "lodash";
export default {
  inject: ["Events"],
  props: {
    descForm: {
      type: Object,
      default: () => {},
    },
    form: {
      type: Object,
      default: () => {},
    },
    config: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      debounceRunjsRule: debounce(RunFormat.runjsRule, 200, { leading: true }),
      showForFun: false,
    };
  },
  watch: {
    seniorFunKey: {
      handler: async function (forms) {
        this.RunjsRule(forms);
      },
      immediate: true,
    },
  },
  computed: {
    RuleConditions() {
      return this.config?.__highSetUp?.RuleConditions || [];
    },
    vmEvents() {
      if (!!this.Events) {
        return this.Events();
      }
    },
    seniorShow() {
      if (
        !this.RuleConditions.length &&
        !this.config.__highSetUp?.DisplayConditions?.length
      )
        return true;
      return (
        this.showForFun &&
        RunFormat.init(
          this.form,
          this.config,
          storeform.getters.getRuleFormData
        )
      );
    },
    seniorFunKey() {
      //函数显隐控制 关联组件字段收集
      return RunFormat.getjsRulekeys(
        this.RuleConditions,
        storeform.getters.getRuleFormData
      );
    },
  },
  components: { ruleItem, rowLayout },
  methods: {
    RunjsRule(forms) {
      this.debounceRunjsRule(
        { forms },
        this.RuleConditions,
        this.vmEvents,
        (flag) => {
          this.showForFun = flag;
        }
      );
    },
  },
};
</script>
